/**
 *
 */
package com.wedroid.r2d2.message.parser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.JsonUtils;
import com.wedroid.entity.message.MessageType;
import com.wedroid.r2d2.contact.Contact;
import com.wedroid.r2d2.message.RawMessage;
import com.wedroid.r2d2.message.RecommendInfo;
import com.wedroid.r2d2.resource.ResourceManager;

/**
 * @author lute
 */
@Component("recommendationContentParser")
public class RecommendationContentParser implements ContentParser {

    @Autowired
    private ResourceManager resourceManager;

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.message.parser.ContentParser#supportsMessageType(com.wedroid.entity.message.MessageType)
     */
    public boolean supportsMessageType(MessageType messageType) {
        return MessageType.VERIFICATION.equals(messageType) || MessageType.SHARECARD.equals(messageType);
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.message.parser.ContentParser#parse(com.wedroid.r2d2.message.RawMessage, java.lang.String)
     */
    public String parse(RawMessage rawMessage, String escapedContent) {
        RecommendInfo recommendInfo = rawMessage.getRecommendInfo();

        // Download recommendation icon
        Contact recommendee = new Contact(recommendInfo);
        String iconPath = resourceManager.requestAvatar(recommendee);
        recommendInfo.setHeadImgUrl(iconPath);

        return JsonUtils.toJsonString(recommendInfo);
    }

}
